package idcard;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * 身份证图片生成
 */
public class IDCardGeneratorFront {
    private final IDCardInfo idCard;
    public static final String outPath = "idcard.png";//这个是临时文件名
    public static final String outDir = "E:\\project\\UnitTool\\src\\main\\java\\idcard\\idcard\\";
    public static final int canvasWidth = 480;
    public static final int canvasHeight = 320;
    //    public static final int imgWidth = 270;
//    public static final int imgHeigth = 428;
    public static final int xStart = 60;
    public static final int yStart = 85;
    public static final int yStep = 32;
    public static final int xStep = 90;
    //    public static final int charSpace = 10;
    public static final int paramXStart = 110;
    public static final int nationSpace = 140;
    public static final int monthSpace = 120;
    public static final int daySpace = 60;
    public static final int idNoSpace = 80;
    public static final int charLineSpace = 25;


    public IDCardGeneratorFront(IDCardInfo idCard) {
        if (null == idCard) {
            throw new RuntimeException("IDCardInfo object can not be null!");
        }
        this.idCard = idCard;
    }

    public void paint() {
        BufferedImage bi = new BufferedImage(canvasWidth, canvasHeight,
                BufferedImage.TYPE_INT_ARGB);//构造一个类型为预约义图像类型之一的 BufferedImage

//        BufferedImage bi = ImageIO.read(
//                new File("E:\\project\\UnitTool\\src\\main\\java\\idcard\\model.png"));

        Graphics2D g2d = getTemplateFront(bi);
        Font font = new Font("华文细黑", Font.BOLD, 16);//可不定义
        g2d.setPaint(Color.black);//给写入的我的信息文本设置为黑色
        g2d.setFont(font);
        int ypStep;
        int xpStep;
        //姓名参数
        g2d.drawString(idCard.getName(), paramXStart, yStart);
        //性别参数
        ypStep = yStart + yStep;
        g2d.drawString(idCard.getSex(), paramXStart, ypStep);

        //民族参数
        xpStep = xStart + nationSpace;
        g2d.drawString(idCard.getNation(), xpStep, ypStep);

        //出生参数
        ypStep += yStep;
        g2d.drawString(idCard.getBornYear(), paramXStart, ypStep);
        xpStep = xStart + monthSpace;
        g2d.drawString(idCard.getBornMonth(), xpStep, ypStep);
        xpStep += daySpace;
        g2d.drawString(idCard.getBornDay(), xpStep, ypStep);

        //住址参数
        ypStep += yStep;
        g2d.drawString(idCard.getAddr1(), paramXStart, ypStep);
        //地址换行处理
        g2d.drawString(idCard.getAddr2(), paramXStart, ypStep + charLineSpace);


        //身份证号
        ypStep += idNoSpace;
        xpStep = xpStep - daySpace;
        font = new Font(Font.MONOSPACED, Font.BOLD, 20);
        g2d.setFont(font);
        g2d.drawString(idCard.getIdNo(), xpStep, ypStep + 10);

        g2d.dispose();
        try {
            ImageIO.write(bi, "PNG", new File(outDir + outPath));
            // 使用支持给定格式的任意 ImageWriter 将一个图像写入// File
            File imgFile = new File(outDir +outPath);//经过将给定路径名字符串转换为抽象路径名来建立一个新 File 实例。
            String imgName = idCard.getIdNo();
            imgFile.renameTo(new File(outDir + imgName + ".jpg"));//从新命名此抽象路径名表示的文件。
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private Graphics2D getTemplateFront(BufferedImage bi) {//该方法是用来获取模板样式

        Graphics2D g2d = bi.createGraphics();
        int ypStep;
        int xpStep;

        //设置背景
        g2d.setBackground(Color.white);//白色背景
        g2d.clearRect(0, 0, canvasWidth, canvasHeight);
//        g2d.fillRect(0, 0, canvasWidth, canvasWidth);


        //绘制圆角矩形
        g2d.setPaint(Color.black);//矩形边框颜色
        RoundRectangle2D rr = new RoundRectangle2D.Double(30, 30, 428, 270, 20, 20);//设置圆角
        g2d.draw(rr);//要呈现的轮廓

        //姓名
        g2d.setPaint(Color.cyan);//这是为模板字体设置为蓝色
        g2d.drawString("姓名", xStart, yStart);//首字符的基线位于用户空间的1�7 (x, y) 位置处1�7

        ypStep = yStart + yStep;
        //性 别
        g2d.drawString("性 别", xStart, ypStep);
        //pStep += yStep;
        //民 族
        xpStep = xStart + xStep;
        g2d.drawString("民 族", xpStep, ypStep);
        ypStep += yStep;
        //出 生
        g2d.drawString("出 生", xStart, ypStep);
        //ypStep += yStep;
        xpStep = xStart + xStep;
        //年
        g2d.drawString("年", xpStep, ypStep);
        //月
        xpStep += xStart;
        g2d.drawString("月", xpStep, ypStep);
        //日
        xpStep += xStart;
        g2d.drawString("日", xpStep, ypStep);
        //住 址
        ypStep += yStep;
        g2d.drawString("住 址", xStart, ypStep);
        ypStep += idNoSpace;
        //号码
        g2d.drawString("公民身份号码", xStart, ypStep + 10);


        //绘制圆角矩形
        g2d.setPaint(Color.black);//矩形边框颜色
        RoundRectangle2D img = new RoundRectangle2D.Double(320, 70, 100, 150, 5, 5);//设置圆角
        g2d.draw(img);//要呈现的轮廓
        //设置头像
//        try {
//            //Image img = ImageIO.read(new File("D:/gen/qie2.png"));
//            Image img = ImageIO.read(new File(".\\qie2.png"));
//            g2d.drawImage(img, 300, 70, null);
//        } catch (IOException e) {
//        }


        return g2d;
    }

    public static void main(String[] args) {
        IDCardInfo idCard = new IDCardInfo();
        idCard.setName("赵一鹏");
        idCard.setNation("汉");
        idCard.setAddr("陕西省渭南市大荔县黄河镇黄家沟二组");
        idCard.setIdNo("622426199908080018");
        IDCardGeneratorFront idCardFront = new IDCardGeneratorFront(idCard);
        idCardFront.paint();
        System.out.println("complete!!!");
    }
}
